#include <unistd.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <setjmp.h>

#include "cJSON.h"

using namespace std;

jmp_buf env;

std::string get_content(const char * file_path) {
    std::string content;
    if(access(file_path, 0) != 0) {
        cout << "File not found" << endl;
        return "";
    }

    FILE *fp = fopen(file_path, "rb");
    if (fp == NULL) { return ""; }
    int ch;
    while((ch = fgetc(fp)) != EOF) {
        content.push_back(ch);
    }
    fclose(fp);
    return content;
}

int main() {
    cout << "cJSON_Version: " << cJSON_Version() << endl;

    std::string json_path = "test.json";
    std::string json_str = get_content(json_path.c_str());
    auto ptr = cJSON_Parse(json_str.c_str());

    std::string key1 = "name";
    auto name_ptr = cJSON_GetObjectItem(ptr, key1.c_str());

    std::string key2 = "age";
    auto age_ptr = cJSON_GetObjectItem(ptr, key2.c_str());

    cout << key1 << ": " << name_ptr->valuestring << ", "
         << key2 << ": " << age_ptr->valueint << "\n"
         << ptr->string << endl;
    return 0;
}